package checkx

import (
	"os"
	"regexp"
	"strings"
)

// IsValidPhoneNumber 验证手机号码是否有效
func IsValidPhoneNumber(phoneNumber string) bool {
	if phoneNumber == "" {
		return true
	}
	// 中国手机号码的正则表达式
	phoneRegex := `^1[3-9]\d{9}$`
	re := regexp.MustCompile(phoneRegex)
	return re.MatchString(phoneNumber)
}

// IsValidEmail 验证电子邮件地址是否有效
func IsValidEmail(email string) bool {
	if email == "" {
		return true
	}
	// 简单的电子邮件正则表达式
	const emailRegex = `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
	re := regexp.MustCompile(emailRegex)
	return re.MatchString(email)
}

func IsValidCompare(str string, compare string) bool {
	res := strings.Compare(str, compare)
	return res == 0
	//return str == compare
}

// PathExists 判断所给路径文件/文件夹是否存在
func PathExists(path string, isCreate bool) bool {
	if path == "" {
		return false
	}
	_, err := os.Stat(path)
	if err == nil {
		return true
	}

	if os.IsNotExist(err) {
		if isCreate {
			cleanedPath := strings.TrimRight(path, "/")
			err = os.MkdirAll(cleanedPath, 0755)

		}
		return true
	}

	return true
}
